'use strict';

module.exports = {
  up: async (queryInterface, Sequelize) => {
    await queryInterface.createTable('clothing_tags', {
      id: {
        type: Sequelize.INTEGER,
        primaryKey: true,
        autoIncrement: true,
      },
      clothing_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '衣服ID',
        references: {
          model: 'clothing',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      tag_id: {
        type: Sequelize.INTEGER,
        allowNull: false,
        comment: '标签ID',
        references: {
          model: 'tags',
          key: 'id',
        },
        onUpdate: 'CASCADE',
        onDelete: 'CASCADE',
      },
      created_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
      updated_at: {
        type: Sequelize.DATE,
        allowNull: false,
        defaultValue: Sequelize.NOW,
      },
    });

    // 添加索引
    await queryInterface.addIndex('clothing_tags', ['clothing_id']);
    await queryInterface.addIndex('clothing_tags', ['tag_id']);
    await queryInterface.addIndex('clothing_tags', {
      fields: ['clothing_id', 'tag_id'],
      unique: true,
      name: 'unique_clothing_tag',
    });
  },

  down: async (queryInterface, Sequelize) => {
    await queryInterface.dropTable('clothing_tags');
  },
};
